package com.threeonemeeting.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.threeonemeeting.R;
import com.threeonemeeting.model.MessageDetail;

public class MessageDetailListAdapter extends ArrayAdapter<MessageDetail> {

	private ArrayList<MessageDetail> mDetailItemsList;

	public void setDetailList(ArrayList<MessageDetail> detailList) {
		mDetailItemsList = detailList;
		notifyDataSetChanged();
	}

	public ArrayList<MessageDetail> getDetailList() {
		return mDetailItemsList;
	}

	public void insert(MessageDetail detail) {
		if (mDetailItemsList == null)
			mDetailItemsList = new ArrayList<MessageDetail>();

		mDetailItemsList.add(0, detail);
	}

	@Override
	public void add(MessageDetail detail) {
		if (mDetailItemsList == null)
			mDetailItemsList = new ArrayList<MessageDetail>();

		mDetailItemsList.add(detail);
	}

	public void clear() {
		if (mDetailItemsList != null) {
			mDetailItemsList.clear();
		}
	}

	@Override
	public int getCount() {
		if (mDetailItemsList == null)
			return 0;

		return mDetailItemsList.size();
	}

	@Override
	public MessageDetail getItem(int position) {
		return mDetailItemsList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public MessageDetailListAdapter(Activity activity) {
		super(activity, 0);
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		Activity activity = (Activity) getContext();
		MessageDetail messageInfo = getItem(position);

		View rowView = convertView;
		MessageDetailCache viewCache;

		LayoutInflater inflater = activity.getLayoutInflater();
		rowView = inflater.inflate(messageInfo.nLayoutID, null);
		viewCache = new MessageDetailCache(rowView, messageInfo.strDate,
				messageInfo.strText);
		rowView.setTag(viewCache);

		// 发送消息的时间
		TextView senderTimeView = viewCache.getMessageTimeTextView();
		senderTimeView.setText(messageInfo.strDate);

		// 发送消息的内容
		TextView senderMsgView = viewCache.getMessageMsgTextView();
		senderMsgView.setText(Html.fromHtml(messageInfo.strText));

		return rowView;
	}

	public class MessageDetailCache {

		private View baseView;
		private String strMessageTime;
		private String strMessageContent;

		private TextView messageTimeTextView;
		private TextView messageContentTextView;

		public MessageDetailCache(View baseView, String strMessageTime,
				String strMessageContent) {
			this.baseView = baseView;

			this.strMessageTime = strMessageTime;
			this.strMessageContent = strMessageContent;
		}

		public String getSenderTime() {
			return this.strMessageTime;
		}

		public String getSenderMsg() {
			return this.strMessageContent;
		}

		public TextView getMessageTimeTextView() {
			if (messageTimeTextView == null) {
				messageTimeTextView = (TextView) baseView
						.findViewById(R.id.message_time);
			}
			return messageTimeTextView;
		}

		public TextView getMessageMsgTextView() {
			if (messageContentTextView == null) {
				messageContentTextView = (TextView) baseView
						.findViewById(R.id.message_content);
			}
			return messageContentTextView;
		}
	}
}
